package com.sk.cardeal.act.home.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.cardeal.R;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.utils.SnackUtils;
import com.sk.cardeal.view.TitleView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sk on 2018/3/13.
 */

public class ChangePasswordActivity extends BaseActivity implements HandleDataCallBack {
    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.old_password)
    EditText oldPassword;
    @BindView(R.id.new_password)
    EditText newPassword;
    @BindView(R.id.confirm_password)
    EditText confirmPassword;
    @BindView(R.id.save)
    TextView save;

    private LoadingDialog loadingDialog;

    @Override
    protected int getContentView() {
        return R.layout.activity_change_password;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
        titleView.setMyCallBack(this);
    }

    @Override
    public void clickTitle(int i) {
        super.clickTitle(i);
        switch (i) {
            case 1:
                finish();
                break;
            case 2:
                break;
            case 3:
                break;
        }
    }

    @OnClick(R.id.save)
    public void onViewClicked() {
        if (TextUtils.isEmpty(oldPassword.getText().toString())) {
            SnackUtils.showError(mRootView, "请输入原密码！");
            return;
        }

        if (TextUtils.isEmpty(newPassword.getText().toString())) {
            SnackUtils.showError(mRootView, "请输入新密码！");
            return;
        }

        if (TextUtils.isEmpty(confirmPassword.getText().toString())) {
            SnackUtils.showError(mRootView, "请确认新密码！");
            return;
        }

        loadingDialog.show();
        AppApi.changePwd(MyUtils.getUser(mContext).getLoginName(), oldPassword.getText().toString(), newPassword.getText().toString(), ChangePasswordActivity.this, 0, ChangePasswordActivity.this);
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        Toast.makeText(mContext,"密码修改成功！",Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }
}
